package puzzle.masyu2.view.swing;

import java.awt.Color;
import java.awt.Graphics2D;

import puzzle.common.point.Direction;
import puzzle.common.view.swing.AbstractPuzzleViewPanel;
import puzzle.masyu2.data.MasyuBoard;

public class MasyuViewPanel extends AbstractPuzzleViewPanel<MasyuBoard> {

	private static final long serialVersionUID = 1L;

	@Override
	protected void paintBoard(Graphics2D g2, MasyuBoard b) {
		g2.setColor(Color.BLACK);

		int r = (int) (m_cellSize * 0.4);
		g2.setStroke(m_thinStroke);

		for (int y = 0; y < b.height; y++) {
			for (int x = 0; x < b.width; x++) {
				int x0 = x * m_cellSize;
				int y0 = y * m_cellSize;
				if (b.isDirty(x, y)) {
					g2.setColor(Color.CYAN);
					g2.fillRect(x0, y0, m_cellSize, m_cellSize);
				}
				
				int x1 = x0 + m_cellSize / 2 - r;
				int y1 = y0 + m_cellSize / 2 - r;

				switch (b.getDangoType(x, y)) {
				case NONE:
					break;
				case KURO:
					g2.setColor(Color.BLACK);
					g2.fillOval(x1, y1, r * 2, r * 2);
					break;
				case SHIRO:
					g2.setColor(Color.WHITE);
					g2.fillOval(x1, y1, r * 2, r * 2);
					g2.setColor(Color.BLACK);
					g2.drawOval(x1, y1, r * 2, r * 2);
					break;
				}
			}
		}
		
		drawGrid(g2, b);

		for (int y = 0; y <= b.height; y++) {
			for (int x = 0; x < b.width; x++) {
				int x0 = x * m_cellSize + m_cellSize / 2;
				int y0 = y * m_cellSize;

				if (b.getExistFlag(x, y, Direction.N)) {
					g2.setStroke(m_lineStroke);
					g2.setColor(b.isAssumed(x, y, Direction.N) ? Color.RED : Color.BLACK);
					g2.drawLine(x0, y0 - m_cellSize / 2, x0, y0 + m_cellSize / 2);
				} else if (b.getForbiddenFlag(x, y, Direction.N)) {
					g2.setColor(Color.BLACK);
					drawCross(g2, x0, y0, m_dotSize);
				}
			}
		}

		for (int y = 0; y < b.height; y++) {
			for (int x = 0; x <= b.width; x++) {
				int x0 = x * m_cellSize;
				int y0 = y * m_cellSize + m_cellSize / 2;

				if (b.getExistFlag(x, y, Direction.W)) {
					g2.setStroke(m_lineStroke);
					g2.setColor(b.isAssumed(x, y, Direction.W) ? Color.RED : Color.BLACK);
					g2.drawLine(x0 - m_cellSize / 2, y0, x0 + m_cellSize / 2, y0);
				} else if (b.getForbiddenFlag(x, y, Direction.W)) {
					g2.setColor(Color.BLACK);
					drawCross(g2, x0, y0, m_dotSize);
				}
			}
		}

	}
}
